<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .box {
            width: 450px;
            height: 260px;
            margin: 50px auto 0;
            border: 1px solid gray;
            position: relative;
        }

        .box>img {
            width: 100%;
            height: 100%;
        }

        .left {
            width: 50px;
            height: 50px;
            position: absolute;
            left: 0;
            top: 0;
            background-image: url(../images/2.jpg);
            background-size: 450px 260px;
            z-index: 9999;
        }

        .right {
            width: 50px;
            height: 50px;
            background-color: #fff;
            position: absolute;
            left: 0;
            top: 0;
        }

        .buttom {
            width: 450px;
            height: 50px;
            margin: 0px auto;
            border: 1px solid gray;
            border-top: none;
            position: relative;
        }

        .circle {
            width: 48px;
            height: 48px;
            border: 1px solid red;
            position: absolute;
            left: 0;
            top: 0;
        }
    </style>
</head>

<body>
    <div class="box">
        <img src="../images/2.jpg" alt="">
        <div class="left"></div>
        <div class="right"></div>
    </div>
    <div class="buttom">
        <div class="circle"></div>
    </div>
</body>
<script>
    var buttom = document.getElementsByClassName("buttom")[0];
    var circle = document.getElementsByClassName("circle")[0];
    var leftB = document.getElementsByClassName("left")[0];
    var right = document.getElementsByClassName("right")[0];
    var box = document.getElementsByClassName("box")[0];

    var sum = box.clientWidth - leftB.clientWidth * 4;
    var exent = Math.floor(Math.random() * sum) + 100;
    var sumTwo = box.clientHeight - leftB.clientHeight * 2;
    var altitude = Math.floor(Math.random() * sumTwo) + 30;

    right.style.left = exent + "px";
    right.style.top = altitude + "px";
    leftB.style.top = altitude + "px";
    leftB.style.backgroundPosition = -exent + "px" + " " + (-altitude) + "px"

    circle.onmousedown = function (event) {
        var event = event || window.event;
        var posX = event.offsetX;

        document.onmousemove = function (event) {
            var event = event || window.event;
            var x = event.pageX - posX - buttom.offsetLeft;

            x = Math.min(Math.max(0, x), box.clientWidth - leftB.clientWidth)
            circle.style.left = x + "px";
            leftB.style.left = x + "px";
        }

        document.onmouseup = function (event) {
            console.log(circle.offsetLeft);
            document.onmousemove = null;
            if (circle.offsetLeft >= exent - 3 && circle.offsetLeft <= exent + 3) {
                location.href = "https://www.baidu.com";
            } else {
                circle.style.left = 0 + "px";
                leftB.style.left = 0 + "px";
            }
        }
    }
</script>

</html>